#!/usr/bin/perl

# LastTurn, a script to determine the last player (or players
# if several missed the cutoff run date) who submitted a turn.
#
# The intent is to track the most frequent tardy player in
# regards to turn submission.
#
# This program is a companion script to the VGAPd and the
# VGAPd CGI addons.

use warnings;
use strict;
use Fcntl;
use Config::IniFiles;
use File::stat;

# Variable Declaration
our %ini;
our @lastsubmitted = (1,1,1,1,1,1,1,1,1,1,1);
my $data;
my $count;
my $inifile;

# Begin Main Body
Usage() if (!defined $ARGV[0]);
if (-e $ARGV[0]) {
  $inifile = shift(@ARGV);
} else {
  print "Specified INI file does not exist!\n\n";
  Usage();
}

tie %ini, 'Config::IniFiles', ( -file => $inifile )
  or die("Could not open INI file $inifile.\n");

if (-e $ini{General}{Homedir}.'lastin.dat') {
  die("Cannot read $ini{General}{Homedir}lastin.dat\n") if (!-r $ini{General}{Homedir}.'lastin.dat');
  die("$ini{General}{Homedir}lastin.dat is not writeable\n") if (!-w $ini{General}{Homedir}.'lastin.dat');
  open (FILE, $ini{General}{Homedir}.'lastin.dat')
    or die("Failed to open $ini{General}{Homedir}lastin.dat\n");
  $data = <FILE>;
  close FILE;
  chomp($data);
  @lastsubmitted = split(/:/,$data) if (defined($data));
  if (-e $ini{General}{Homedir}.'lastin.old') {
    unlink($ini{General}{Homedir}.'lastin.old')
      or die("Failed to delete old backup file!\n");
  }
  rename($ini{General}{Homedir}.'lastin.dat',$ini{General}{Homedir}.'lastin.old')
    or die("Failed to rename .dat file to .old\n");
} elsif (-e $ini{General}{Homedir}.'lastin.old') {
  print "Lastin.dat does not exist, but Lastin.old found.\n
        Recovering from failure and restoring to last count.\n";
  open (FILE, $ini{General}{Homedir}.'lastin.old')
    or die("Failed to open $ini{General}{Homedir}lastin.old\n");
  $data = <FILE>;
  close FILE;
  chomp($data);
  if (defined($data)) {
    @lastsubmitted = split /:/,$data;
  }
}

if (CheckSubmit()) {
  FindLastPlayer();
}
if (!sysopen(FILE, $ini{General}{Homedir}.'lastin.dat', O_RDWR | O_CREAT | O_EXCL)) {
  die("File $ini{General}{Homedir}lastin.dat already exists!");
} else {
  for ($count = 0; $count <= 10; $count++) {
    print FILE $lastsubmitted[$count];
    print FILE ":" if ($count < 10);
  }
  close FILE;
}

untie %ini;
# End Main Body


# Subroutines

sub Usage {
  die("Usage: lastturn.pl /path/to/vgapd/game.ini\n");
} #End Usage sub

sub CheckSubmit {
  my $returnval = 1;
  my $count;
  
  for ($count = 1; $count <= 11; $count++) {
    if (!-e $ini{General}{Homedir}."player$count.trn"
        && (uc($ini{"Player$count"}{Active}) eq "YES"))
    {
      $returnval = 0;
      $lastsubmitted[($count - 1)]++;
    }
  }
  return $returnval;
} #End CheckSubmit sub

sub FindLastPlayer {
  my $count;
  my $lastplayer;
  my $oldestturn;
  
  for ($count = 1; $count <= 11; $count++) {
    if (uc($ini{"Player$count"}{Active}) eq "YES") {
      die("Player$count.trn does not exist! How can this be?!?\n")
        if (!-e $ini{General}{Homedir}."player$count.trn");
      if (!defined($lastplayer)
          || $oldestturn < stat($ini{General}{Homedir}."player$count.trn")->mtime)
      {
        $lastplayer = $count;
        $oldestturn = stat($ini{General}{Homedir}."player$count.trn")->mtime;
      }
    }
  }
  $lastsubmitted[($lastplayer - 1)]++;
} #End FindLastPlayer sub
